<!-- 
   图文结构封装组件
  -->
<template>
  <div>
    <el-table :data="tableData" style="width: 100%" size="mini" border>
      <el-table-column type="index" width="50" label="序号" align="center">
      </el-table-column>
      <el-table-column prop="imageUrl" label="图片" width="180" align="center">
        <template slot-scope="scope">
          <div class="upload-pic" @click="getIndex(scope.$index)">
            <el-upload
              class="avatar-uploader"
              action=""
              :show-file-list="false"
              :before-upload="beforeAvatarUpload"
              :on-error="handleAvatarError"
              :http-request="submitUploadImgUrl"
            >
              <img
                v-if="scope.row.imageUrl"
                :src="scope.row.imageUrl"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span style="color:red;font-size:12px">推荐尺寸： 1065*1065</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="文字描述" prop="description" align="center">
        <template slot-scope="scope">
          <div>
            <el-input
              type="textarea"
              v-model="scope.row.description"
              placeholder="请输入内容"
              maxlength="150"
              show-word-limit
              resize="none"
              :autosize="{ minRows: 6 }"
            ></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center">
        <template slot-scope="scope">
          <div>
            <i class="el-icon-top sort" @click="up(scope.$index)"></i>
            <i class="el-icon-bottom sort" @click="down(scope.$index)"></i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            plain
            style="margin: 5px"
            @click="del(scope.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="add-text">
      <span @click="addLine"
        ><i class="el-icon-plus"></i> 点击添加商品详情图文</span
      >
    </div>
  </div>
</template>
<script>
import axios from "axios";
export default {
  name: "imageText",
  props: ["tableData"],
  data() {
    return {
      url: process.env.VUE_APP_URL + "/sys/oss/upload",
      // url: "https://jsonplaceholder.typicode.com/posts/"
      hideUpload: false,
      imgIndex: 0
    };
  },
  components: {},
  mounted() {},
  methods: {
    getIndex(val) {
      this.imgIndex = val;
    },
    submitUploadImgUrl(uploader) {
      let form = new FormData();
      form.append("file", uploader.file);
      axios
        .post(this.url, form, {
          headers: {
            "Content-Type": "multipart/form-data"
          }
        })
        .then(res => {
          let i = this.imgIndex;
          this.tableData[i].imageUrl = res.data.data.src;
          //上传成功
          //处理自己的逻辑
          this.$message({
            type: "success",
            message: "上传成功！"
          });
        })
        .catch(() => {
          //上传失败
          //处理自己的逻辑
          this.$message({
            type: "error",
            message: "上传错误！"
          });
        });
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isJPEG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isJPG && !isJPEG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG、JPEG、PNG 格式!");
      }
      if (!isLt5M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
      }
      return (isJPG || isJPEG || isPNG) && isLt5M;
    },
    handleAvatarError() {
      this.$message({
        type: "error",
        message: "上传错误！"
      });
    },
    up(index) {
      if (index == 0) {
        this.$message({
          type: "warning",
          message: "已经是第一个了哦！"
        });
      } else {
        var tempOption = this.tableData[index - 1];
        this.$set(this.tableData, index - 1, this.tableData[index]);
        this.$set(this.tableData, index, tempOption);
        this.$emit("handleTableDate", this.tableData);
      }
    },
    down(index) {
      if (index == this.tableData.length - 1) {
        this.$message({
          type: "warning",
          message: "已经是最后一个了哦！"
        });
      } else {
        var tempOption = this.tableData[index + 1];
        this.$set(this.tableData, index + 1, this.tableData[index]);
        this.$set(this.tableData, index, tempOption);
        this.$emit("handleTableDate", this.tableData);
      }
    },
    del(value) {
      this.tableData.splice(value, 1);
      this.$emit("handleTableDate", this.tableData);
    },
    addLine() {
      //添加行数
      var newValue = {
        imageUrl: "",
        description: ""
      };
      //添加新的行数
      this.tableData.push(newValue);
      this.$emit("handleTableDate", this.tableData);
    }
  }
};
</script>
<style scoped lang="scss">
.sort {
  cursor: pointer;
  color: #409eff;
}
.sort:hover {
  color: #027cfb;
}
.add-text {
  text-align: center;
  span {
    cursor: pointer;
    color: #027cfb;
  }
}
</style>
